package top.huxy.boot.util.obj;

import lombok.Data;
import top.huxy.boot.model.dto.Pager;

import java.util.Map;

/**
 * @Classname Pagination
 * @Description Pagination
 * @Date 2021/9/3 8:21
 * @Created by huxy
 */
@Data
public class Pagination {
    private int total;
    private int currPage;
    private int maxPage;
    private int pageSize;
    private int perPage;
    private int needPage;
    private int nextPage;

    public Pagination(Pager pager, int total) {
        this.total = total;
        this.needPage=pager.getPage();

        if (pager.getPage() * pager.getPageSize() > total) {
            currPage = (int) (Math.ceil(1.0 * total / pager.getPageSize()));
        } else {
            currPage = pager.getPage();
        }
        //当total=0时，currPage会是0，所以这里改成1
        if (currPage<1) currPage=1;

        pageSize = pager.getPageSize();
        maxPage = (int) (Math.ceil(1.0 * total / pager.getPageSize()));

        perPage = currPage - 1;
        if (perPage < 1) perPage = 1;

        nextPage=currPage+1;
        if(nextPage>maxPage) nextPage=maxPage;
    }

    @SuppressWarnings("all")
    public Pagination addToMap(Map map){
        map.put("total",total);
        map.put("maxPage",maxPage);
        map.put("currPage",currPage);
        map.put("pageSize",pageSize);
        map.put("perPage",perPage);
        map.put("needPage",needPage);
        map.put("nextPage",nextPage);
        return this;
    }

    public int getIndex() {
        return (currPage - 1) * pageSize;
    }
    public int getEndIndex(){
        return getIndex()+getPageSize()-1;
    }

    public int getLeftIndex(){
        return total-((currPage)*pageSize);
    }
    public int getRightIndex(){
        return total-((currPage-1)*pageSize);
    }
}
